package com.huawei.hmsdemo10086.Adapter;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.huawei.hmsdemo10086.POJO.Video;
import com.huawei.hmsdemo10086.R;

import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder>{


    private List<Video> videoList;
    private Context mContext;

    private OnItemClickLitener mOnItemClickLitener;

    public VideoAdapter(Context context, List<Video> videoList){
        this.mContext = context;
        this.videoList = videoList;
    }

    //设置回调接口
    public interface OnItemClickLitener{
        public void onItemClick(View view, int position);
        public void onCollection(View view, int position);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener){
        this.mOnItemClickLitener = mOnItemClickLitener;
    }


    static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView video_title;
        private ImageView video_img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            video_img = itemView.findViewById(R.id.video_img);
            video_title = itemView.findViewById(R.id.video_title);
        }
    }
    @NonNull

    @Override
    public VideoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull VideoAdapter.ViewHolder holder, int position) {
        holder.video_title.setText(videoList.get(position).getTitle());

        //Glide设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(20, 20);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).skipMemoryCache(true);

        Glide.with(holder.itemView)
                .load(videoList.get(position).getAddress())
                .apply(options)
                .into(holder.video_img);
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickLitener.onItemClick(view, position);
                }
            });
        }
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    @Override
    public int getItemCount() {
        return videoList.size();
    }
}
